/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_logger.h
 * @date:   Jan 26, 2012
 */

#ifndef INSTALLER_UTIL_U_LOGGER_H_
#define INSTALLER_UTIL_U_LOGGER_H_

#include <stdio.h>
#include <stdbool.h>

#include "logger.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_logger_start();                           /* unit entry point */

bool u_installer_logger_new();
bool u_installer_logger_delete();
bool u_installer_logger_log();

bool u_installer_logger_start() {
    return (u_installer_logger_new()
        && u_installer_logger_delete()
        && u_installer_logger_log());
}


bool u_installer_logger_new() {
    char file_name[]   = "/tmp/installer-logger_new";
    FILE *file_pointer = fopen(file_name, "w");

    if (file_pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_logger_new", file_name, strerror(errno));
        return false;
    }

    Logger *logger = installer_logger_new(file_pointer);

    if (logger == NULL) {
        u_installer_main_print("logger", "new()", false);
    }

    installer_logger_delete(logger);
    
    fclose(file_pointer);
    remove(file_name);

    u_installer_main_print("logger", "new()", true);

    return true;
}

bool u_installer_logger_delete() {
    /* unit case is a part of new() */
    u_installer_main_print("logger", "delete()", true);

    return true;
}

bool u_installer_logger_log() {
    char file_name[]   = "/tmp/installer-logger_log";
    FILE *file_pointer = fopen(file_name, "w");

    if (file_pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_logger_log", file_name, strerror(errno));
        return false;
    }

    Logger *logger = installer_logger_new(file_pointer);

    if (logger == NULL) {
        u_installer_main_print("logger", "log()", false);
    }

    installer_logger_log(logger, INFO, "unit", "unit message");
    installer_logger_delete(logger);
    
    fclose(file_pointer);
    remove(file_name);

    u_installer_main_print("logger", "log()", true);

    return true;
}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_U_LOGGER_H_ */ 
